{extend name="platform/base" /} {block name="resources"/}
<script type="text/javascript" src="__STATIC__/My97DatePicker/WdatePicker.js"></script>
<style type="text/css">
.modal-dl dt {
    line-height: 31px;
    float: left;
    font-weight: normal;
    width: 30px;
}
.modal-dl dd {
    float: left;
	margin-left: 10px;
}
dl {
    overflow: hidden;
}
</style>
{/block} {block name="main"}
<div class="row padder-v">
	<div class="col-sm-1"></div>
	<div class="col-sm-2">
		<div class="input-group">
			<input type="text" id="startDate" class="input-sm form-control"
				placeholder="请选择申请时间开始日期" onclick="WdatePicker()" />
		</div>
	</div>
	<div class="col-sm-2">
		<div class="input-group">
			<input type="text" id="endDate" placeholder="请选择申请时间结束日期"
				class="input-sm form-control" onclick="WdatePicker()" />
		</div>
	</div>
	<div class="col-sm-2">
		<div class="input-group">
			<input id="userName" class="input-sm form-control" type="text"
				value="" placeholder="请输入用户姓名">
		</div>
	</div>
	<div class="col-sm-2">
		<div class="input-group">
			<select id="agent_type"
				class="input-sm form-control input-s-sm inline">
				<option value="0">请选择代理类型</option>
				<option value="1">省代理</option>
				<option value="2">市代理</option>
				<option value="3">区代理</option>
			</select>
		</div>
	</div>
	<div class="col-sm-2">
		<div class="input-group">
			<select id="is_audit" class="input-sm form-control input-s-sm inline">
				<option value="10">--选择状态--</option>
				<option value="0">待审核</option>
				<option value="1">通过</option>
				<option value="-1">审核未通过</option>
				<option value="2">取消</option>
			</select>
		</div>
	</div>	
	<div class="col-sm-1">
		<div class="input-group">
			<span class="input-group-btn">
				<button class="btn btn-sm btn-default" type="button"
					onclick="LoadingInfo(1)">搜索</button>
			</span>
		</div>
	</div>
</div>
<section class="panel panel-default">
	<div class="table-responsive">
		<table class="table table-striped b-light text-sm">
			<colgroup>
				<col style="width: 3%;">
				<col style="width: 11%;">
				<col style="width: 11%;">
				<col style="width: 9%;">
				<col style="width: 11%;">
				<col style="width: 11%;">
				<col style="width: 11%;">
				<col style="width: 11%;">
				<col style="width: 11%;">
				<col style="width: 11%;">
			</colgroup>
			<thead>
				<tr>
					<th><input type="checkbox"></th>
					<th>会员信息</th>
					<th>手机号</th>
					<th>代理类型</th>
					<th>代理省份</th>
					<th>代理市</th>
					<th>代理区县</th>
					<th>联系地址</th>
					<th>状态</th>
					<th class="center">申请时间</th>
					<th class="center">操作</th>
				</tr>
			</thead>
			<tbody id="list">
				<tr></tr>
			</tbody>
		</table>
	</div>
	{include file="platform/page" /}
</section>
<!-- 审核区域代理 -->
<div class="modal fade" id="editArea" tabindex="-1" role="dialog"
	aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabel">分配代理区域</h4>
			</div>
			<div class="modal-body">
				<div class="modal-div">
					<input type="hidden" id="region_agent_id" value='' />
					<dl class="modal-dl">
						<dt>省:</dt>
						<dd>
							<select id="province">
								<option value="0">--请选择省--</option>
							</select>
						</dd>
					</dl>
					<dl class="modal-dl" id="city_box">
						<dt>市:</dt>
						<dd>
							<select id="city">
								<option>--请选择市--</option>

							</select>
						</dd>
					</dl>
					<dl class="modal-dl" id="district_box">
						<dt>县:</dt>
						<dd>
							<select id="district">
								<option>--请选择县--</option>
							</select>
						</dd>
					</dl>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-success"
					onclick="regionSure();">提交</button>
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal -->
</div>
{/block} {block name="script"}
<script type="text/javascript">	
var $province;
var $city;
var $district;
//查询
$(function() {
	LoadingInfo(1);
});
$(function() {
	$.ajax({
	     url: "{:__URL('PLATFORM_MAIN/Distribution/getAddressList')}",
	     type: "post",   
	     success: function (res) { 
	    	 $province=res.province_list;
	    	 $city=res.city_list;
	    	 $district=res.district_list;	    	 
	    	 $.each($province,function(k, c) {
						var $option = "<option value=' "+ c.province_id + "'>"
								+ c.province_name
								+ "</option>";
						$("#province").append($option);
					
				});
	     }	
	 	});	
		$("#province").change(function () {			
	        var province_id = $(this).val();
	        $("#city option:gt(0)").remove();
	        $("#district option:gt(0)").remove();
	        var $cityArray=new Array();
	        for($i=0;$i<$city.length;$i++){
	        	if(parseInt($city[$i].province_id)==parseInt(province_id)){
	        		$cityArray[$cityArray.length]=$city[$i];
	        	}	        		
	        }
	        $.each($cityArray, function (k, c) { 
	         var $option = "<option value=' "+ c.city_id + "'>" + c.city_name + "</option>";
	         $("#city").append($option);                        
	     	});	
	    });
		$("#city").change(function () {
	        var city_id = $(this).val();                        
	        $("#district option:gt(0)").remove();
	        var $districtArray=new Array();
	        for($i=0;$i<$district.length;$i++){
	        	if(parseInt($district[$i].city_id)==parseInt(city_id)){
	        		$districtArray[$districtArray.length]=$district[$i];
	        	}
	        		
	        }
	        $.each($districtArray, function (k, d) { 
	        	var $option = "<option value=' "+ d.district_id + "'>" + d.district_name + "</option>";
	            $("#district").append($option);                     
	     	});	
	     });			   	     	 
})
	//加载数据
	function LoadingInfo(pageIndex) {
	var user_name = $("#userName").val();
	var start_date = $("#startDate").val();
	var end_date = $("#endDate").val();
	var agent_type = $("#agent_type").val();
	var is_audit = $("#is_audit").val();
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/Distribution/promoterRegionAgentList')}",
			async : false,
			data : {
				"pageIndex" : pageIndex,
				"user_name":user_name,
				"start_date" : start_date, 
				"end_date" : end_date,
				"is_audit" : is_audit,
				"agent_type":agent_type
			},
			success : function(data) {
				//alert(JSON.stringify(data));
				var html = '';
				$("#total_count_num").text(data["total_count"]);
				$("#page_count_num").text(data["page_count"]);
				$("#page_count").val(data["page_count"]);
				$("#pageNumber a").remove();
				if (data["data"].length > 0) {
					for (var i = 0; i < data["data"].length; i++) {
						var agent_type= "";
						if(data["data"][i]["agent_type"] == 1){
							var agent_type = "省代";
						}else if(data["data"][i]["agent_type"] == 2 ){
							var agent_type = "市代";
						}else{
							var agent_type = "区县代理";
						}
						var audit_status = "";
						if(data["data"][i]["is_audit"] == 0){
							audit_status = "待审核";
						}else if(data["data"][i]["is_audit"] == 1 ){
							audit_status = "通过";
						}else if(data["data"][i]["is_audit"] == -1 ){
							audit_status = "审核未通过";
						}else{
							audit_status = "取消";
						}
						html += '<tr>';
						html += '<td><div class="cell"><label ><input name="sub" type="checkbox" value="'+ data["data"][i]["region_agent_id"] +'" ></label></div></td>';
						if(data["data"][i]["promoter_no"]==null){
							html += '<td>' + data["data"][i]["real_name"] + '<br/></td>';
						}else{
							html += '<td>' + data["data"][i]["real_name"] + '<br/>' + data["data"][i]["promoter_no"] + '</td>';
						}
						
						html += '<td>' + data["data"][i]["agent_mobile"] + '</td>';
						html += '<td>' + agent_type + '</td>';
						html += '<td>' + data["data"][i]["province_name"] + '</td>';
						html += '<td>' + data["data"][i]["city_name"] + '</td>';
						html += '<td>' + data["data"][i]["district_name"] + '</td>';
						html += '<td>' + data["data"][i]["agent_address"] + '</td>';
						html += '<td>' + audit_status + '</td>';
						html += '<td align="center">' + timeStampTurnTime(data["data"][i]["reg_time"]) + '</td>';
						html +='<td align="center">';
						if(data["data"][i]["is_audit"] == "0"){
							html += '<a href="javascript:void(0);" onclick="promoterAudit('+ data["data"][i]["agent_id"] +','+data["data"][i]["agent_type"]+',1);">通过</a>&nbsp;&nbsp;<a href="javascript:void(0);" onclick="promoterAudit('+ data["data"][i]["agent_id"] +','+data["data"][i]["agent_type"]+',-1);">拒绝</a> ';
						}else if(data["data"][i]["is_audit"] == "1"){
							html += '<a href="javascript:void(0);" onclick="promoterEdit('+ data["data"][i]["agent_id"] +','+ data["data"][i]["agent_provinceid"] +','+ data["data"][i]["agent_cityid"] +','+ data["data"][i]["agent_districtid"] +', '+data["data"][i]["agent_type"]+');">修改</a> ';
							//html += '<a href="javascript:void(0);" onclick="promoterCancel('+ data["data"][i]["agent_id"] +');">取消</a> ';
						    html += '<a href="javascript:void(0);" onclick="removeRegionAgent('+ data["data"][i]["agent_id"] +');">取消资格</a> ';
						}else if(data["data"][i]["is_audit"] == "2"){
							//html += '<a href="javascript:void(0);" onclick="promoterCancel('+ data["data"][i]["region_agent_id"] +');">恢复</a> ';
						}
						html +='</td>';
						html += '</tr>';
					}
				} else {
					html += '<tr align="center"><th colspan="9">暂无符合条件的数据记录</th></tr>';
				}
				
				$("#list").html(html);
				var totalpage = $("#page_count").val();
				if (totalpage == 1) {
					changeClass("all");
				}
				var $html = pagenumShow(jumpNumber,totalpage,{$pageshow})
				$("#pageNumber").append($html);
			}
		});
	}	
	//区域代理审核通过
	function promoterAudit(region_agent_id, agent_type, is_audit){
		if(is_audit == 1){
			$( "#dialog" ).dialog({				
	            buttons: {
	                "通过,#e57373": function() {
	                	//audit(region_agent_id,1);
	                    $(this).dialog('close');
	                	regionChoice(region_agent_id, agent_type);
	                },
	                "取消": function() {
	                    $(this).dialog('close');
	                }
	            },
	            contentText:"确定要通过此用户的区域代理审核吗？",
			});			
		}else if(is_audit == -1){
			$( "#dialog" ).dialog({				
	            buttons: {
	                "拒绝,#e57373": function() {
	                	audit(region_agent_id,is_audit);
	                    $(this).dialog('close');
	                	//regionChoice(region_agent_id, agent_type);
	                },
	                "取消": function() {
	                    $(this).dialog('close');
	                }
	            },
	            contentText:"确定要拒绝此用户的区域代理审核吗？",
			});	
		}
	}
	function promoterEdit(region_agent_id, agent_provinceid, agent_cityid, agent_districtid, agent_type){
		$("#editArea").modal("show");
		$("#district_box").show();
		$("#city_box").show();
		$("#province").val(0);
		$("#district").val(0);
		$("#city").val(0);
		$("#region_agent_id").val(region_agent_id);
		$("#agent_type").val(agent_type);
		if(agent_type == 1){
			$("#district_box").hide();
			$("#city_box").hide();
		}else if((agent_type == 2)){
			$("#district_box").hide();
		}	
		
			$("#province").val(agent_provinceid);
			$("#province option").each(function(){
				if($(this).val() == agent_provinceid){
					$(this).attr("selected", "selected");
					return;
				}
			})
			$("#province").trigger('change');	
			$("#city option").each(function(){
				if($(this).val() == agent_cityid){
					$(this).attr("selected", "selected");
					return;
				}
			})
			$("#city").trigger('change');
			$("#district option").each(function(){
				if($(this).val() == agent_districtid){
					$(this).attr("selected", "selected");
					return;
				}
			})
			
	}
	//区域代理审核取消
	/* function promoterCancel(region_agent_id){
		$( "#dialog" ).dialog({				
            buttons: {
                "确定,#e57373": function() {
                	audit(region_agent_id,2);
                    $(this).dialog('close');
                },
                "取消": function() {
                	//audit(promoter_id,-1);
                    $(this).dialog('close');
                }
            },
            contentText:"确定要取消此用户的区域代理审核吗？",
		});
	} */
	function audit(region_agent_id,is_audit){
		if(is_audit == 1){
			var province_id = $("#province").val();
			var city_id = $("#city").val();
			var district_id = $("#district").val();
		}
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/Distribution/modifyPromoterRegionAgentIsAudit')}",
			async : true,
			data : {
				"region_agent_id" : region_agent_id,
				"is_audit":is_audit,
				"province_id":province_id,
				"city_id":city_id,
				"district_id":district_id
			},
			success : function(data) {				
				if(data.code == 1){
					showMessage('success', "审核成功！","{:__URL('PLATFORM_MAIN/Distribution/modifyPromoterRegionAgentIsAudit')}");	
					location.reload();
				}else{
					showMessage('error', "同一级代理一片区域只能分配一次！");	
					$("#editArea").modal("hide");
				}
			}
    	})
	}
	/**
	**区域选择框
	*/
	function regionChoice(region_agent_id, agent_type){
		$("#region_agent_id").val(region_agent_id);
		$("#editArea").modal("show");
		$("#district_box").show();
		$("#city_box").show();
		$("#province").val(0);
		$("#district").val(0);
		$("#city").val(0);
		if(agent_type == 1){
			$("#district_box").hide();
			$("#city_box").hide();
		}else if((agent_type == 2)){
			$("#district_box").hide();
		}			
	}
	
	function regionSure(){
		var region_agent_id = $("#region_agent_id").val();
		audit(region_agent_id,1);
	}
	
	//区域代理审核取消
	function removeRegionAgent(region_agent_id){
		$( "#dialog" ).dialog({				
            buttons: {
                "确定,#62c462": function() {
                	removePromoterRegionAgent(region_agent_id);
                    $(this).dialog('close');
                },
                "取消,#e57373": function() {
                	//audit(promoter_id,-1);
                    $(this).dialog('close');
                }
            },
            contentText:"确定要取消此用户的区域代理资格吗？",
		});
	}
	function removePromoterRegionAgent(region_agent_id){
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/distribution/removepromoterregionagent')}",
			async : true,
			data : {
				"region_agent_id" : region_agent_id
			},
			success : function(data) {
				if(data.code == 1){
					showMessage('success', "区域代理资格取消成功！");	
					location.reload();
				}
			}
		})
	}
</script>
{/block}
